package com.dfrz.bookstore.dao.impl;



import com.dfrz.bookstore.dao.Dao;
import com.dfrz.bookstore.entry.Admin;
import com.dfrz.bookstore.util.JdbcUtils;

import java.sql.*;
import java.util.List;

/**
 * 类功能描述:
 *
 * @Author keyan
 * @Date 2021/5/14 19:17
 */
public class AdminDaoImpl implements Dao<Admin> {

    public Admin login(Admin admin){
        Connection conn = null;
        ResultSet resultSet = null;
        PreparedStatement prep = null;
        String sql = "select * from admin where name = ? and password = ? ";
        try {
            conn =  JdbcUtils.getConnection();
            prep = conn.prepareStatement(sql);
            prep.setString(1,admin.getUsername());
            prep.setString(2,admin.getPassword());
            resultSet = prep.executeQuery();
            if (!resultSet.next()){
                return null;
            }
            Admin admin1 = new Admin();
            admin1.setId(resultSet.getInt("id"));
            admin1.setUsername(resultSet.getString("name"));
            admin1.setPassword(resultSet.getString("password"));
            return admin1;

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                JdbcUtils.close(conn,prep,resultSet);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    @Override
    public boolean insert(Admin admin)   {
        return false;
    }

    @Override
    public boolean deleteObjById(int i) {
        return false;
    }


    @Override
    public boolean updateByObj(Admin admin) {
        if (admin.getId()==0){
            return false;
        }
        String sql = "update admin set id = "+admin.getId();
        if (admin.getUsername()!=null){
            sql+=" , name = '"+admin.getUsername()+"'";
        }

        if (admin.getPassword()!=null){
            sql+=" , password = '"+admin.getPassword()+"'";
        }
        Connection conn = null;
        Statement stat = null;
        try {
            conn = JdbcUtils.getConnection();
            stat = conn.createStatement();
            boolean falg = stat.execute(sql);
            return falg;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Admin> findObjByObj(Admin admin) {
        return null;
    }

    @Override
    public List<Admin> findObjByPage(Admin admin,int pageNum, int pageSize)   {
        return null;
    }
}
